using System.Linq;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Criterion;
using Managistics.Model;
using Managistics.Utility.Common;

namespace Managistics.Core.Repositories.Impl
{
    public class OutputRepository : RepositoryBase<OutputSheet, string>, IOutputRepository
    {
        private readonly IGoodRepository _goodRepository;

        public OutputRepository(IGoodRepository goodRepository)
        {
            this._goodRepository = goodRepository;
        }

        /// <summary>
        /// Get all parity goods has outputed in month/year
        /// </summary>
        /// <param name="month">The month of year</param>
        /// <param name="year">The year</param>
        /// <returns></returns>
        public IList<OutputSheetDetail> GetParityGoodsOutputedInMonth(int month, int year)
        {
            OutputSheet outputSheet = null;

            var query =
                this.Session.QueryOver<OutputSheetDetail>()
                .JoinAlias(x => x.OutputSheet, () => outputSheet)
                .WhereRestrictionOn(x => x.ItemId).IsIn(this._goodRepository.GetStringParityGoods())
                .And(Restrictions.Eq(
                    Projections.SqlFunction("year",
                    NHibernate.NHibernateUtil.Int32,
                    Projections.Property<OutputSheet>(o => outputSheet.OutputSheetDate)), year))
                .And(Restrictions.Eq(
                    Projections.SqlFunction("month",
                    NHibernate.NHibernateUtil.Int32,
                    Projections.Property<OutputSheet>(o => outputSheet.OutputSheetDate)), month));

            return query.List<OutputSheetDetail>();
        }

    }
}